﻿using System;
using System.Data.Entity;
using System.Linq;
using Core.Contract.DataContract;
using www.Models;

namespace Data.DAO
{
    public class StoryRefDAO//:BaseDAO<RELAXEntities,RelaxStory>
    {
        #region value
        private RelaxDataContext _context = new RelaxDataContext();
        #endregion

        #region Ham lay du Lieu

        public IQueryable<RelaxStoryDataContract> GetStoryDataContracts()
        {
                
            var query = (from c in _context.NEWS_REFs
                        join v in _context.NEWS_VALUEs on c.id equals v.newid
                        //join ct in _context.CATEGORY_VALUEs on c.categoryId equals ct.categoryid
                        join l in _context.RelaxLogins on c.userId equals l.id
                select new RelaxStoryDataContract
                {
                    Id = Convert.ToInt32(c.id),
                    Image = c.image,
                    Date = Convert.ToDateTime(c.date),
                    Priority = Convert.ToInt32(c.priority),
                    CategoryId =Convert.ToInt32(c.categoryId),
                    Active = Convert.ToInt32(c.active),
                    IsDefault = Convert.ToInt32(c.isdefault),
                    Name = v.name,
                    Tag = v.tag,
                    Keyword = v.keyword,
                    Decription = v.decription,
                    Detail = v.detail,
                    //CategoryName = ct.name,
                    View = Convert.ToInt32(c.view ?? 0),
                    Userid = Convert.ToInt32(c.userId ?? 0),
                    UserName = l.username,
                    Languagecode = Convert.ToInt32(v.languagecode),
                    Report = Convert.ToInt32(c.report ?? 0),
                    UserBrower = Convert.ToInt32(c.userbrower ?? 0)
                }).Distinct();
            return query;

        }

        public IQueryable<RelaxStoryDataContract> GetStoryDataContract()
        {
            var query = (from c in _context.NEWS_REFs
                        join v in _context.NEWS_VALUEs on c.id equals v.newid
                        join ct in _context.CATEGORY_VALUEs on c.categoryId equals ct.categoryid
                join m in _context.RelaxMembers on c.userId equals m.id
                        select new RelaxStoryDataContract
                        {
                            Id = Convert.ToInt32(c.id),
                            Image = c.image,
                            Date = Convert.ToDateTime(c.date),
                            Priority = Convert.ToInt32(c.priority),
                            CategoryId = Convert.ToInt32(c.categoryId),
                            Active = Convert.ToInt32(c.active),
                            IsDefault = Convert.ToInt32(c.isdefault),
                            Name = v.name,
                            Tag = v.tag,
                            Keyword = v.keyword,
                            Decription = v.decription,
                            Detail = v.detail,
                            CategoryName = ct.name,
                            View = Convert.ToInt32(c.view ?? 0),
                            Userid = Convert.ToInt32(c.userId ?? 0),
                            UserName = m.name,
                            Languagecode = Convert.ToInt32(v.languagecode),
                            Report = Convert.ToInt32(c.report ?? 0),
                            UserBrower = Convert.ToInt32(c.userbrower ?? 0)
                        }).Distinct();
            return query;
        }
        public IQueryable<RelaxStoryDataContract> GetStoryId()
        {
            var query = from c in _context.NEWS_REFs
                        select new RelaxStoryDataContract
                        {
                            Id = Convert.ToInt32(c.id),
                            Image = c.image,
                            Date = Convert.ToDateTime(c.date),
                            Priority = Convert.ToInt32(c.priority),
                            CategoryId = Convert.ToInt32(c.categoryId),
                            Active = Convert.ToInt32(c.active),
                            IsDefault = Convert.ToInt32(c.isdefault),
                            View = Convert.ToInt32(c.view ?? 0),
                            Userid = Convert.ToInt32(c.userId ?? 0)
                        };
            return query;
        }
        #endregion 
    
       
        #region Add
        public void Add(NEWS_REF story)
        {
            _context.NEWS_REFs.InsertOnSubmit(story);
            _context.SubmitChanges();

        }
        #endregion
        #region Ham Update

        public void Update(NEWS_REF story)
        {
            var firstOrdefault = _context.NEWS_REFs.FirstOrDefault(c => c.id == story.id);
            if (firstOrdefault!=null)
            {
                firstOrdefault.date = story.date;
                firstOrdefault.priority = story.priority;
                firstOrdefault.image = story.image;
                firstOrdefault.categoryId = story.categoryId;
                firstOrdefault.active = story.active;
                firstOrdefault.isdefault = story.isdefault;
                firstOrdefault.userId = story.userId;
                firstOrdefault.report = story.report;
                firstOrdefault.userbrower = story.userbrower;
                _context.SubmitChanges();
            }
        }
        public void UpdateView(long id)
        {
            var first = _context.NEWS_REFs.FirstOrDefault(p => p.id == id);
            if (first != null)
            {
                first.view = first.view + 1;
                _context.SubmitChanges();
            }
        }
        #endregion 

        #region Ham Delete

        public void Delete(long iId)
        {
            var query = _context.NEWS_REFs.FirstOrDefault(c => c.id == iId);
            if (query!=null)
            {
                // ReSharper disable once AssignNullToNotNullAttribute
                _context.NEWS_REFs.DeleteOnSubmit(query);
                _context.SubmitChanges();
            }
        }
        #endregion
    }
}
